package com.datasource.util;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class DataTool
{	
	private static Logger logger = LogManager.getLogger( DataTool.class );
	
	public static List rsToList( ResultSet rs ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList();
        while( rs.next() )
        {
        	HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
                row.put(rsmd.getColumnName(i),rs.getObject(i));
            }
            rs_data.add(row);
        }
        return rs_data;
	}
	public static List rsToList( ResultSet rs, boolean addKH) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList();
        while( rs.next() )
        {
        	HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
                row.put(rsmd.getColumnName(i),rs.getObject(i));
            }
            rs_data.add(row);
        }
        return rs_data;
	}
	public static List rsToList( ResultSet rs, int pageSize ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList( pageSize );
        int rowNumber = 0;
        while( rs.next() && rowNumber < pageSize )
        {
        	HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
                row.put(rsmd.getColumnName(i),rs.getObject(i));
            }
            rs_data.add(row);
            rowNumber ++;
        }
        return rs_data;
	}
	
	public static LinkedList rsToLinkedList( ResultSet rs ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        LinkedList rs_data = new LinkedList();
        while( rs.next() )
        {
        	LinkedHashMap row = new LinkedHashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
                   	 || rsmd.getColumnType(i) == Types.VARCHAR 
                   	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
               	{
            		//row.put(rsmd.getColumnName(i),"'"+rs.getString(i)+"'");modify by yhh
            		row.put(rsmd.getColumnName(i),rs.getString(i));
               	}
                else{
                	row.put(rsmd.getColumnName(i),rs.getObject(i));
                }
            }
            rs_data.add(row);
        }
        return rs_data;
	}
	
	public static LinkedList rsToLinkedList( ResultSet rs, int pageSize ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        LinkedList rs_data = new LinkedList();
        int rowNumber = 0;
        while( rs.next() && rowNumber < pageSize )
        {
        	LinkedHashMap row = new LinkedHashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
                row.put(rsmd.getColumnName(i),rs.getObject(i));
            }
            rs_data.add( row );
            rowNumber ++;
        }
        return rs_data;
	}
	
	public static HashMap rsToMap( ResultSet rs ) throws SQLException 
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		HashMap row = null;
        if( rs.next() )
        {
        	row = new HashMap( columnCount, 1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
                row.put( rsmd.getColumnName(i), rs.getObject(i) );
            }
        }
        return row;
	}
	
	public static HashMap rsToMap( ResultSet rs, String sourceEncoding,String targetEncoding ) throws SQLException 
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		HashMap row = null; 
        if( rs.next() )
        {
        	row = new HashMap( columnCount, 1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
                   	 || rsmd.getColumnType(i) == Types.VARCHAR 
                   	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
               	{
               		String temp = rs.getString(i);
               		if( temp != null )
               		{
               			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
               		}else
               		{
               			temp = "";
               		}
               		row.put(rsmd.getColumnName(i),temp);
               	}else
               	{
               		row.put(rsmd.getColumnName(i),rs.getObject(i));
               	}
            }
        }
        return row;
	}
	
	public static List rsToList( ResultSet rs,String sourceEncoding,String targetEncoding ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList();
        while( rs.next() )
        {
            HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
            	 || rsmd.getColumnType(i) == Types.VARCHAR 
            	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
            	{
            		String temp = rs.getString(i);
            		if( temp != null )
            		{
            			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
            		}else
            		{
            			temp = "";
            		}
            		row.put(rsmd.getColumnName(i),temp);
            	}else
            	{
            		row.put(rsmd.getColumnName(i),rs.getObject(i));
            	}
            }
            rs_data.add(row);
        }
        return rs_data; 
	}
	public static List rsToList( ResultSet rs,String sourceEncoding,String targetEncoding,boolean addKH ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList();
        while( rs.next() )
        {
            HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
            	 || rsmd.getColumnType(i) == Types.VARCHAR 
            	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
            	{
            		String temp = rs.getString(i);
            		if( temp != null )
            		{
            			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
            		}else
            		{
            			temp = "";
            		}
            		if(addKH)
            			row.put(rsmd.getColumnName(i),"'"+temp+"'");
            		else
            		    row.put(rsmd.getColumnName(i),temp);
            	}else
            	{
            		row.put(rsmd.getColumnName(i),rs.getObject(i));
            	}
            }
            rs_data.add(row);
        }
        return rs_data; 
	}	
	
	public static List rsToList( ResultSet rs, int pageSize, String sourceEncoding,String targetEncoding ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        List rs_data = new ArrayList( pageSize );
        int rowNumber = 0; 
        while( rs.next() && rowNumber < pageSize )
        {
            HashMap row = new HashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
            	 || rsmd.getColumnType(i) == Types.VARCHAR 
            	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
            	{
            		String temp = rs.getString(i);
            		if( temp != null )
            		{
            			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
            		}else
            		{
            			temp = "";
            		}
            		row.put(rsmd.getColumnName(i),temp);
            	}else
            	{
            		row.put(rsmd.getColumnName(i),rs.getObject(i));
            	}
            }
            rs_data.add(row);
            rowNumber ++; 
        }
        return rs_data; 
	}
	
	public static LinkedList rsToLinkedList( ResultSet rs,String sourceEncoding,String targetEncoding ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        LinkedList rs_data = new LinkedList();
        while( rs.next() )
        {
            LinkedHashMap row = new LinkedHashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
            	 || rsmd.getColumnType(i) == Types.VARCHAR 
            	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
            	{
            		String temp = rs.getString(i);
            		if( temp != null )
            		{
            			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
            		}else
            		{
            			temp = "";
            		}
            		row.put(rsmd.getColumnName(i),temp);
            	}else
            	{
            		row.put(rsmd.getColumnName(i),rs.getObject(i));
            	}
            }
            rs_data.add(row);
        }
        return rs_data; 
	}
	
	public static LinkedList rsToLinkedList( ResultSet rs, int pageSize, String sourceEncoding,String targetEncoding ) throws SQLException
	{
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
        LinkedList rs_data = new LinkedList();
        int rowNumber = 0;
        while( rs.next() && rowNumber < pageSize )
        {
            LinkedHashMap row = new LinkedHashMap( columnCount,1 );
            for( int i = 1; i <= columnCount; i ++ )
            {
            	if( rsmd.getColumnType(i) == Types.CHAR 
            	 || rsmd.getColumnType(i) == Types.VARCHAR 
            	 || rsmd.getColumnType(i) == Types.LONGVARCHAR )
            	{
            		String temp = rs.getString(i);
            		if( temp != null )
            		{
            			try
						{
							temp = new String( temp.getBytes( targetEncoding ), sourceEncoding ).trim();
						} catch (UnsupportedEncodingException e)
						{
							e.printStackTrace();
						}
            		}else
            		{
            			temp = "";
            		}
            		row.put(rsmd.getColumnName(i),temp);
            	}else
            	{
            		row.put(rsmd.getColumnName(i),rs.getObject(i));
            	}
            }
            rs_data.add(row);
            rowNumber ++;
        }
        return rs_data; 
	}
	
	public static void showListContent( List listData )
	{
		if( listData.size() > 0 )
		{
	    	for( int i=0; i<listData.size(); i ++ )
	    	{
    			HashMap row = (HashMap)listData.get(i);
    			Set keys = row.keySet();
    			String valueStr = new String();
    			for(Iterator e = keys.iterator(); e.hasNext(); )
    			{
    				String name = (String) e.next();
    				String value = null;
    				Object field = row.get(name);
    				if( field != null )
    				{
    					value = field.toString();
    				}else
    				{
    					value = "";
    				}
    				valueStr += name+":"+value+" ";
    			}
		    	System.out.println( valueStr );
	    	}
		}
	}
	
	public static void showMapContent( HashMap singleRow )
	{
		Set keys = singleRow.keySet();
		String valueStr = new String();
		for(Iterator e = keys.iterator(); e.hasNext(); )
		{
			String name = (String) e.next();
			String value = null;
			Object field = singleRow.get(name);
			if( field != null )
			{
				value = field.toString();
			}else
			{
				value = "";
			}
			valueStr += name+":"+value+" ";
		}
    	System.out.println( valueStr );
	}
}
